package com.cskaoyan.connectionpool;



import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 创建日期: 2022/04/22 16:19
 *
 * @author ciggar
 */
public class DBCPUtils {

    // 数据库连接池
    static DataSource dataSource;

    static {

        try {
            Properties properties = new Properties();
            FileInputStream fileInputStream = new FileInputStream("dbcp.properties");
            properties.load(fileInputStream);

            // 初始化数据库连接池 DBCP
            dataSource = BasicDataSourceFactory.createDataSource(properties);

        }catch (Exception ex) {
            ex.printStackTrace();
        }


    }



    // 获取连接
    public static Connection getConnection(){

        Connection connection = null;
        try {
            connection =   dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return connection;

    }


}
